package com.rbs.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rbs.common.Constants;
import com.rbs.entity.CustDistrict;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.rbs.common.Result;
import java.util.List;

import com.rbs.service.IDepartmentService;
import com.rbs.entity.Department;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author lisl
 * @since 2024-04-07
 */
@RestController
@RequestMapping("/department")
@Validated
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    //新增/修改
    @PostMapping("/saveAndUpdate")
    public Result saveAndUpdate(@RequestBody Department department){
        if (department.getDStrname()!="") {
            boolean flag=departmentService.findSameDepartName(department);
            if (flag==false){
                //部门名不存在，保存新的
                return Result.success(departmentService.saveOrUpdate(department));
            }else {
                //部门名存在
                return Result.error("部门名已经存在");
            }
        }else {
            return Result.error(Constants.CODE_400,"部门名不能为空！");
        }
    }

    //按名称模糊查询
    //传空值时返回全部
    @GetMapping("findByName")
    public Result findByName(@RequestBody Department department){
        List<Department> dList=departmentService.findByName(department);
        return Result.success(dList);
    }

    //展示所有部门信息
    @GetMapping("/findAll")
    public Result findAll() {
        List<Department> listDepartments = departmentService.list();
        return Result.success(listDepartments);
    }

    //按id删除部门
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        return Result.success(departmentService.removeById(id));
    }

    //批量删除
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        return Result.success(departmentService.removeBatchByIds(ids));
    }

    //查询单个
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(departmentService.getById(id));
    }

    //按id排序，true从大到小，false从小到大
    @GetMapping("/orderBy")
    public Result descByID(@RequestParam boolean descOrAsc,
                           @RequestParam String column,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize){
        //选哪一列来排序
        if (column.equals("dStrname"))column="D_strName";

        QueryWrapper<Department> queryWrapper = new QueryWrapper<>();
        if (descOrAsc==true){
            queryWrapper.orderByDesc(column);
        }else if (descOrAsc==false){
            queryWrapper.orderByAsc(column);
        }
        return Result.success(departmentService.page(new Page<>(pageNum,pageSize),queryWrapper));
    }

}

